
#include <BCone>
#include <BCube>
#include <BCapsule>
#include <BCylinder>
#include <BSphere>
#include <BGeoSphere>
#include <BTube>

#include <BPainter>
#include <BRender>
#include <BViewer>
#include <BScene>
#include <BImage>
#include <BMaterial>
#include <BReadWrite>

#include <BLabel>
#include <BGridLayout>

#include "SupportVertexPanel.h"

SupportVertexPanel::SupportVertexPanel()
{
	_shapeComboBox = new BComboBox();
	_shapeComboBox->addItem("Cube", new BCube);
	_shapeComboBox->addItem("Cone", new BCone(1, 2));
	_shapeComboBox->addItem("Capsule", new BCapsule);
	_shapeComboBox->addItem("Cylinder", new BCylinder(1, 5));
	_shapeComboBox->addItem("Sphere", new BSphere);
	_shapeComboBox->addItem("GeoSphere", new BGeoSphere());
	_shapeComboBox->addItem("Tube", new BTube);
	_shapeComboBox->setCurrentIndex(0);

	_lengthSpinBox = new BFloatSpinBox();
	_lengthSpinBox->setValue(1);

	_vertexXSpinBox = new BFloatSpinBox();
	_vertexYSpinBox = new BFloatSpinBox();
	_vertexZSpinBox = new BFloatSpinBox();

	_rotateRollSpinBox = new BFloatSpinBox(-PIf, PIf);
	_rotateRollSpinBox->setArrowVisible(true);
	_rotateRollSpinBox->setStep((float)0.1);
	_rotateRollSpinBox->setSuffix("[-PI ~ PI]");
	_rotateRollSpinBox->setSuffixAlign(Align_RightCenter);

	_rotatePitchSpinBox = new BFloatSpinBox(-PIf / 2, PIf / 2);
	_rotatePitchSpinBox->setArrowVisible(true);
	_rotatePitchSpinBox->setStep((float)0.1);
	_rotatePitchSpinBox->setSuffix("[-PI/2 ~ PI/2]");
	_rotatePitchSpinBox->setSuffixAlign(Align_RightCenter);

	_rotateYawSpinBox = new BFloatSpinBox(-PIf, PIf);
	_rotateYawSpinBox->setArrowVisible(true);
	_rotateYawSpinBox->setStep((float)0.1);
	_rotateYawSpinBox->setSuffix("[-PI ~ PI]");
	_rotateYawSpinBox->setSuffixAlign(Align_RightCenter);

	BGridLayout* gridLayout = new BGridLayout(this);
	gridLayout->addWidget(0, 0, new BLabel("shape"));
	gridLayout->addWidget(0, 1, _shapeComboBox);
	gridLayout->addWidget(1, 0, new BLabel("length"));
	gridLayout->addWidget(1, 1, _lengthSpinBox);
	gridLayout->addWidget(2, 0, new BLabel("vertex"));
	gridLayout->addWidget(2, 1, _vertexXSpinBox);
	gridLayout->addWidget(3, 1, _vertexYSpinBox);
	gridLayout->addWidget(4, 1, _vertexZSpinBox);
	gridLayout->addWidget(5, 0, new BLabel("roll"));
	gridLayout->addWidget(5, 1, _rotateRollSpinBox);
	gridLayout->addWidget(6, 0, new BLabel("pitch"));
	gridLayout->addWidget(6, 1, _rotatePitchSpinBox);
	gridLayout->addWidget(7, 0, new BLabel("yaw"));
	gridLayout->addWidget(7, 1, _rotateYawSpinBox);

	gridLayout->setColumnStretch(0, 0);

	connect(_shapeComboBox, Signal_ValueChanged, &SupportVertexPanel::slotShapeComboBoxValueChanged);
	connect(_rotateRollSpinBox, Signal_EditFinished, &SupportVertexPanel::slotShapeRotationEditFinished);
	connect(_rotatePitchSpinBox, Signal_EditFinished, &SupportVertexPanel::slotShapeRotationEditFinished);
	connect(_rotateYawSpinBox, Signal_EditFinished, &SupportVertexPanel::slotShapeRotationEditFinished);
}
SupportVertexPanel::~SupportVertexPanel()
{

}

void SupportVertexPanel::setShape(BShape* shape)
{
	_shapeComboBox->setValue(shape);
}
BShape* SupportVertexPanel::shape()
{
	return _shapeComboBox->value();
}

void SupportVertexPanel::setLength(BReal length)
{
	_lengthSpinBox->setValue((float)length);
}
BReal SupportVertexPanel::length() const
{
	return _lengthSpinBox->value();
}

void SupportVertexPanel::setVertex(const BVector& vertex)
{
	_vertexXSpinBox->setValue((float)vertex.x());
	_vertexYSpinBox->setValue((float)vertex.y());
	_vertexZSpinBox->setValue((float)vertex.z());
}
BVector SupportVertexPanel::vertex() const
{
	BReal x = _vertexXSpinBox->value();
	BReal y = _vertexYSpinBox->value();
	BReal z = _vertexZSpinBox->value();
	return BVector(x, y, z);
}

void SupportVertexPanel::setRotation(BReal roll, BReal pitch, BReal yaw)
{
	_rotateRollSpinBox->setValue((float)roll);
	_rotatePitchSpinBox->setValue((float)pitch);
	_rotateYawSpinBox->setValue((float)yaw);
}
void SupportVertexPanel::setRotation(const BVector& rotation)
{
	_rotateRollSpinBox->setValue((float)rotation.x());
	_rotatePitchSpinBox->setValue((float)rotation.y());
	_rotateYawSpinBox->setValue((float)rotation.z());
}
BVector SupportVertexPanel::rotation() const
{
	float roll = _rotateRollSpinBox->value();
	float pitch = _rotatePitchSpinBox->value();
	float yaw = _rotateYawSpinBox->value();
	return BVector(roll, pitch, yaw);
}

void SupportVertexPanel::slotShapeComboBoxValueChanged(BObject* object, const BValue& value)
{
	this->emit("shape-changed", value);
}
void SupportVertexPanel::slotShapeRotationEditFinished(BObject* object, const BValue& value)
{
	float roll = _rotateRollSpinBox->value();
	float pitch = _rotatePitchSpinBox->value();
	float yaw = _rotateYawSpinBox->value();
	this->emit("rotation-changed", BVector(roll, pitch, yaw));
}
